package com.gwcloud.design.create.builder;

/** 
* @ClassName   : BuilderPattern 
* @author      : ganwei 
* @date        : 2020年7月12日 
* @Description : 构造者模式，当一个类的构造函数参数个数超过4个，而且这些参数有些是可选的参数，考虑使用构造者模式 
*/
public class BuilderPattern {
	private String param1;      //必选
	private String param2;      //必选
	private String param3;      //可选
	private String param4;      //可选
	
	private BuilderPattern(Builder builder) {
		this.param1 = builder.param1;
		this.param2 = builder.param2;
		this.param3 = builder.param3;
		this.param4 = builder.param4;
	}
	
	
	public static class Builder{
		private String param1;      //必选
		private String param2;      //必选
		private String param3;      //可选
		private String param4;      //可选
		
        public Builder(String param1,String param2){
            this.param1 = param1;
            this.param2 = param2;
        }
        
        public Builder setParam3(String param3) {
        	this.param3 = param3;
        	return this;
        }
    
        public Builder setParam4(String param4) {
        	this.param4 = param4;
        	return this;
        }
        
        public BuilderPattern build(){
            return new BuilderPattern(this);
        }
	}
	
	
	/**
	 * @return the param1
	 */
	public String getParam1() {
		return param1;
	}
	/**
	 * @param param1 the param1 to set
	 */
	public void setParam1(String param1) {
		this.param1 = param1;
	}
	/**
	 * @return the param2
	 */
	public String getParam2() {
		return param2;
	}
	/**
	 * @param param2 the param2 to set
	 */
	public void setParam2(String param2) {
		this.param2 = param2;
	}
	/**
	 * @return the param3
	 */
	public String getParam3() {
		return param3;
	}
	/**
	 * @param param3 the param3 to set
	 */
	public void setParam3(String param3) {
		this.param3 = param3;
	}
	/**
	 * @return the param4
	 */
	public String getParam4() {
		return param4;
	}
	/**
	 * @param param4 the param4 to set
	 */
	public void setParam4(String param4) {
		this.param4 = param4;
	}
	
	public void printParams() {
		System.out.println("================");
		System.out.println(param1);
		System.out.println(param2);
		System.out.println(param3);
		System.out.println(param4);
		System.out.println("================");
	}
	
	public static void main(String[] args) {
		BuilderPattern bp = new BuilderPattern.Builder("必选1", "必选2").setParam3("可选3").setParam4("可选4").build();
		bp.printParams();
	}
	
}
